package com.ziyun.pop.mall.system.aop;

import com.ziyun.pop.mall.module.time.action.UserActionTake;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

/**
 * @Author:chenxiaoyi
 * @Date:2018/11/5 17:50
 */
@Aspect
@Component
public class PopWebRequestPrinter {

	private final static Logger log = LoggerFactory.getLogger(PopWebRequestPrinter.class);

	@Pointcut("execution(* com.ziyun.pop.mall.module..*.*(..))")
	public void toPrint() {
	}

	@Before(value = "toPrint()")
	public void doBefore() throws Throwable {
		ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
		if (attributes == null) {
			return;
		}
		HttpServletRequest request = attributes.getRequest();
		String method = request.getMethod();
		String url = request.getRequestURI();
		UserActionTake.getInstance().addUserTake(url, method);
	}
}